<?php
	class dBConnect {
		
		private $mydbName;
		private $myhost;
		private $myuser;
		private $mypasswrd;
		
		//set up database object
		function dBConnect(){
			$this->mydbName = "teamcal";
			$this->myhost = "mysql.padillatech.com";
			$this->myuser = "jaanj1";
			$this->mypasswrd = "starbucks";
		}
			
		function connect(){
			$db = mysqli_connect($this->myhost, $this->myuser, $this->mypasswrd);
			mysqli_select_db($db, $this->mydbName);
				
			if($db)	
				return $db;
			else
				return false;
		}
		
		//returns an array of requested tuples if successful query else returns an empty array
		function getData($data, $table, $conditions = '', $order = ''){
			$db = $this->connect();
			
			$rtn = array();
			$sql = "select $data from $table";
			
			if($conditions != '')
				$sql .= " where $conditions";
			
			if($order != '')
				$sql .= " order by $order";	
			
			
			@$result = mysqli_query($db, $sql);
			//inefficient but allows me to take out all database related syntax in all other code
			if($result){
				while($row = mysqli_fetch_assoc($result)){
					$rtn[] = $row;
				}
			}
				
			$this->close($db);
			
			return $rtn;
		}
		
		
		function insert($data, $table, $getid = false, $duplicate=''){
			$db = $this->connect();
			
			$sql = "insert $table values ($data)";
			if($duplicate !='')
				$sql .= " ON DUPLICATE KEY UPDATE ".$duplicate;
				
			$result = mysqli_query($db, $sql);
			$lastid = mysqli_insert_id ($db);
			
			$this->close($db);
			
			if($getid)
				return $lastid;
				
			return $result;
		}
		
		
		function remove($data, $table){
			$db = $this->connect();
			
			$result = mysqli_query($db, "delete from $table where $data");
			
			$this->close($db);	
			
			return $result;
		}
		
		function update($table, $data, $conditions){
			$db = $this->connect();
			$result = mysqli_query($db, "update $table set $data where $conditions");
			
			$this->close($db);	
			
			return $result;
		}
		
		function close($db){
			@mysqli_close($db);
		}
		
	}
?>
